package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.evaluate.Evaluate;
import com.dongdongshop.pojo.goods.TbGoods;
import com.dongdongshop.pojo.goods_desc.TbGoodsDesc;
import com.dongdongshop.pojo.item.TbItem;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.service.ItemPageService;
import com.dongdongshop.service.evaluate.EvaluateService;
import com.dongdongshop.service.goods.GoodsService;
import com.dongdongshop.service.goods_desc.GoodsDescService;
import com.dongdongshop.service.goods_desc_jpa.GoodsDecsJpaService;
import com.dongdongshop.service.goods_jpa.GoodsJpaService;
import com.dongdongshop.service.item.ItemService;
import com.dongdongshop.service.itemCat.ItemCatService;
import com.dongdongshop.service.item_jpa.ItemJpaService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemPageServiceImpl implements ItemPageService {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    @Reference
    private GoodsService goodsService;
    @Reference
    private GoodsDescService goodsDescService;
    @Reference
    private ItemCatService itemCatService;
    @Reference
    private ItemService itemService;
    @Reference
    private EvaluateService evaluateService;
    @Reference
    private GoodsJpaService goodsJpaService;
    @Reference
    private GoodsDecsJpaService goodsDecsJpaService;
    @Reference
    private ItemJpaService itemJpaService;
    @Value("${freemarker.outPath}")
    private String outPath;
    @Override
    public void createItemPage(Long goodsId) {
        //加载配置,创建模板
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        FileWriter fileWriter = null;
        try {
            Template template = configuration.getTemplate("item.ftl");
            //创建数据模型
            Map map = new HashMap();
            /*TbGoods tbGoods = goodsService.getGoodsById(goodsId);*/
            TbGoods tbGoods = goodsJpaService.getGoodsById(goodsId);
            /*TbGoodsDesc tbGoodsDesc = goodsDescService.getGoodsDescById(goodsId);*/
            TbGoodsDesc tbGoodsDesc = goodsDecsJpaService.getGoodsDescById(goodsId);
            List<TbItemCat> tbItemCatList = itemCatService.listCategory(tbGoods.getCategory3Id());
           /* List<TbItem> itemList = itemService.listItemByGoodsId(goodsId);*/
            List<TbItem> itemList = itemJpaService.listItemByGoodsId(goodsId);
            fileWriter = new FileWriter(outPath+goodsId+".html");
            map.put("goods",tbGoods);
            map.put("goodsDesc",tbGoodsDesc);
            map.put("itemCatList",tbItemCatList);
            map.put("itemList",itemList);
            template.process(map,fileWriter);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (fileWriter!=null){
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
